package ca.quicko.webapp.action;

import java.io.ByteArrayInputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.model.User;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextRenderer;

import ca.quicko.model.Issue;
import ca.quicko.service.IssueManager;
import ca.quicko.webapp.html.ModelRenderer;

/**
 * @author <a href="mailto:eric.s.landry@gmail.com">Eric S. Landry</a>
 */
public class IssueAction extends MyBaseAction {
	private static final long serialVersionUID = 2140366669014479190L;

	private Log log = LogFactory.getLog(getClass());

	private IssueManager issueManager;

	private List issues;

	private Issue issue;

	private Long id;

	public void setIssueManager(IssueManager issueManager) {
		this.issueManager = issueManager;
	}

	public Issue getIssue(Long id) {
		return issueManager.get(id);
	}

	public List getIssues() {
		return issues;
	}
	
	public String listIssues() {
		issues = issueManager.getAll();
		return SUCCESS;
	}
	
	public String viewIssue() {
		logState("view");
		if (id != null) {
			issue = issueManager.get(id);
			return SUCCESS;
		}
		return ERROR;
	}

	public String editIssue() {
		logState("edit");
		if (id != null) {
			issue = issueManager.get(id);
		}
		else {
			issue = new Issue();
		}
		return INPUT;
	}
	
	public String saveIssue() {
		logState("save");
		boolean isNew = (issue.getId() == null);
		User currentUser = this.getLoggedInUser();
		issue.setId(id);
		issue.setUser(currentUser);
		issue.setDate(new Date());
		issue.setActive(true);
		issue = issueManager.save(issue);
		id = issue.getId();
		String key = (isNew) ? "issue.added" : "issue.updated";
		saveMessage(getText(key));
		return SUCCESS;
	}

	public String deleteIssue() {
		issueManager.remove(id);
		saveMessage(getText("issue.deleted"));
		return ERROR;
	}
	
	public String cancel() {
		log.warn("cancel");
		return ERROR;
	}

	public String pdf() {
		try {
			String renderedIssue = getRenderedIssue();
			String html = ModelRenderer.wrapWithHtmlTag(renderedIssue);
			getResponse().setContentType("application/pdf");
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			ByteArrayInputStream str = new ByteArrayInputStream(html.getBytes());
			Document doc = builder.parse(str);

			ServletOutputStream out = getResponse().getOutputStream();
			ITextRenderer renderer = new ITextRenderer();
			renderer.setDocument(doc, null);
			renderer.layout();
			renderer.createPDF(out);
			out.flush();
		}
		catch (Exception e) {
			log.error(e, e);
			return ERROR;
		}
		return null;
	}
	
	private void logState(String msg) {
		log.warn(msg + " id : " + id + "  issue " + issue);
	}
	
	public String getRenderedIssue() {
		issue = issueManager.get(id);
		return ModelRenderer.renderIssue(issue);
	}

	public Issue getIssue() {
		return issue;
	}

	public void setIssue(Issue issue) {
		this.issue = issue;
	}

	public void setId(Long id) {
		this.id = id;
	}
}
